﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Traceability_Management.ErrorCode;

namespace Traceability_Management.Read.Api.Controllers.Update
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FileallController : ControllerBase
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async  Task<ApiResult<string>> Fileup(IFormFile File)
        {
        string ML=Directory.GetCurrentDirectory() + "/Files/" + File.FileName;
        using (FileStream fs = new FileStream(ML, FileMode.Create))
        {
            File.CopyTo(fs);
                fs.Flush();
                fs.Close();
        }
            return new ApiResult<string>()
            {
                Code=ApiEnum.Success,
                Data="http://localhost:5094/Files/"+File.FileName
            };


            //var path = "";

            //var fileinfo = Request.Form.Files;
            //List<string> Filetype = new List<string>()
            //{
            //    "image/jpeg",
            //    "image/jpg",
            //    "image/png",
            //};
            //foreach (var itms in fileinfo)
            //{
            //    if (!Filetype.Contains(itms.ContentType))
            //    {
            //        return Ok(-100);
            //    }

            //    if (itms.Length > 2 * 1024 * 1024)
            //    {
            //        return Ok(-200);
            //    }
            //    var ML = Directory.GetCurrentDirectory() + "/Files/" + itms.FileName;
            //    using (FileStream fs = new FileStream(ML, FileMode.Create))
            //    {
            //        itms.CopyTo(fs);
            //    }
            //    path = "http://localhost:5062/Files/" + itms.FileName;
            //}
            //return Ok(path);
        }
    }
}
